import { invoke } from '@tauri-apps/api/core';
import { DeleteCondition, DeleteOptions, QueryCondition, QueryExtention, QueryOptions, ComparsionOperator, UpdateCondition, UpdateOptions } from '../../local/types';
import optionsWrap from '@/components/datasets/create/website/base/options-wrap';
import { DataSource } from '@/services/data-source';

import { API_BASE_URL, getToken, request, uuid } from '@/utils';

export const g01DSFindOne = async (classIdentifier: string, conditions: QueryCondition, extentions?: any, options?: any): Promise<any> => {
    let token = getToken();
    let response = await request({
        url: `${API_BASE_URL}/ds/findone/${classIdentifier}`,
        method: 'POST',
        headers: {
            'Authorization': token
        },
        dataType: 'application/json',
        data: {
            conditions: conditions,
            extentions: extentions,
            options: options,

        }
    });
    const res = await response.json();

    console.log(res);
    return res;
}

export const g01DSFind = async (classIdentifier: string, conditions: QueryCondition, extentions?: QueryExtention, options?: any)
    : Promise<{ total: number, data: Array<any> }> => {
    console.log(classIdentifier, conditions, extentions, options);

    let token = getToken();
    let response = await request({
        url: `${API_BASE_URL}/ds/find/${classIdentifier}`,
        method: 'POST',
        headers: {
            'Authorization': token
        },
        dataType: 'application/json',
        data: {
            conditions: conditions,
            extentions: extentions,
            options: options,

        }
    });

    let text = await response.text();
    try {
        // const res = await response.json();
        const res = JSON.parse(text);
        console.log(res);
        return res;
    } catch (err) {
        console.log(err);
        console.log(text);
    }
    return { total: 0, data: [] }
}

export const g01DSBatchFind = async (items: Array<any[]>, options?: any)
    : Promise<{ total: number, data: Array<any> }> => {
    console.log(items, options);
    let token = getToken();
    let response = await request({
        url: `${API_BASE_URL}/ds/batch/find`,
        method: 'POST',
        headers: {
            'Authorization': token
        },
        dataType: 'application/json',
        data: items
    });
    const res = await response.json();
    console.log(res);
    return res;
}

export const g01DSBatchInsert = async (items: Array<any[]>, options?: any)
    : Promise<any> => {
    console.log(items, options);
    let token = getToken();
    let response = await request({
        url: `${API_BASE_URL}/ds/batch/insert`,
        method: 'POST',
        headers: {
            'Authorization': token
        },
        dataType: 'application/json',
        data: items
    });
    const res = await response.json();
    console.log(res);
    return res;
}

export const g01DSRestInsert = async (classIdentifier: string, values: { [key: string]: any }, options?: any)
    : Promise<any> => {
    console.log(classIdentifier, options);
    let token = getToken();
    try {
        let response = await request({
            url: `${API_BASE_URL}/ds/rest/${classIdentifier}`,
            method: 'POST',
            headers: {
                'Authorization': token
            },
            dataType: 'application/json',
            data: values
        });

        let text = await response.text();
        try {
            const res = JSON.parse(text);
            console.log(res);
            return res;
        } catch (error) {
            console.error(error, text);
        }
    } catch (error) {
        console.error(error);
    }
    return null;
}

export const g01DSRestDeleteById = async (classIdentifier: string, id: number | string)
    : Promise<any> => {
    console.log(classIdentifier, id);
    let token = getToken();
    let response = await request({
        url: `${API_BASE_URL}/ds/rest/${classIdentifier}/${id}`,
        method: 'DELETE',
        headers: {
            'Authorization': token
        },
    });
    const res = await response.json();
    console.log(res);
    return res;
}


export const g01DSRestUpdate = async (classIdentifier: string, id: number | string, values: { [key: string]: any })
    : Promise<any> => {
    console.log(classIdentifier, values);
    let token = getToken();
    let response = await request({
        url: `${API_BASE_URL}/ds/rest/${classIdentifier}/${id}`,
        method: 'PUT',
        headers: {
            'Authorization': token
        },
        dataType: 'application/json',
        data: values
    });
    const res = await response.json();
    console.log(res);
    return res;
}

